package com.chtw.vuedemo.service.impl;

import com.chtw.vuedemo.bean.Comments;
import com.chtw.vuedemo.bean.CommentsExample;
import com.chtw.vuedemo.mapper.CommentsMapper;
import com.chtw.vuedemo.service.inf.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author chtw
 * @date 2020-10-04 17:08
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentsMapper commentsMapper;


    @Override
    public List<Comments> list(int page, int id) {
        PageHelper.startPage(page, 10);
        CommentsExample example = new CommentsExample();
        CommentsExample.Criteria criteria = example.createCriteria();
        criteria.andPIdEqualTo(id);
        List<Comments> comments = commentsMapper.selectByExample(example);
        PageInfo<Comments> info = new PageInfo<>(comments);
        return info.getList();
    }

    @Override
    public boolean create(Comments comments) {
        if(comments.getCreateTime() == null){
            comments.setCreateTime(new Date());
        }
        // 从session获取用户名称
        if("".equals(comments.getUserName()) || comments.getUserName() == null){
            comments.setUserName("匿名用户");
        }
        return commentsMapper.insert(comments) > 0;
    }

}
